import React,{useEffect,useState} from 'react'
import {View,Text,Button} from 'react-native'
import "../global"
import * as ImagePicker from 'expo-image-picker'
import axios from 'axios'

const TestPage = () => {
    const formdata = new FormData();
    const [selectImg,setSelectImg] = useState('');
    const pickImage = async () => {
        let result = await ImagePicker.launchImageLibraryAsync({
            mediaTypes: ImagePicker.MediaTypeOptions.All,
            allowsEditing: true,
            aspect: [4, 3],
            quality: 1,
            base64:true
        });
        if (!result.cancelled) {
            // console.log(result.base64);
            var file = {
                uri:result.base64,
                type:result.type,
                name:'image.jpg'
            }
            formdata.append('file',file)
            setSelectImg(result.uri);
            fetch('http://82.157.94.91:8000/topics/upload',{
                method:'post',
                body:formdata,
                headers:{
                    'Authorization':'Bearer ' + token,
                    'Content-Type':'multipart/form-data;boundary=01051xcz1000921cksxcs'
                }
            }).then(res=>{
                console.log(res);
            }).catch(err=>{
                console.log(err)
            })
            // await axios.post('http://82.157.94.91:8000/topics/upload',formdata,{
            //     'Authorization':'Bearer ' + token,
            //     'Content-type':'multipart/form-data'
            // })
            // .then((response) => {
            //     console.log(response);
            // }).catch((e) => {
            //     console.log(e);
            // });

        }
    };
  
    useEffect(()=>{
        // fetch('http://82.157.94.91:8000/quesiton/upload_img',{
        //     body:formdata,
        //     method:'post',
        //     headers:{},
        // }).then(res => res.json()).then(res=>console.log(res)).catch(err=>{console.log(err)});
        //1
        
        //2
    //     axios({
    //         headers: {
    //             'Content-Type': 'multipart/form-data',
                // 'Authorization':'Bearer ' + token
    //         },
    //         method: 'post',
    //         url: 'http://82.157.94.91:8000/topics/upload',
    //         data: formdata
    //     })
    //     .then(res => res.json())
    //     .then(res=>{
    //         console.log(res);
    //     })
    //     .catch(err=>console.log(err))
    // },[selectImg])
    //axios({
    //     headers: {
    //         'Content-Type': 'multipart/form-data',
    //         'Authorization':'Bearer ' + token
    //     },
    //     method: 'post',
    //     url: 'http://82.157.94.91:8000/topics/upload',
    //     data: formdata
    // })
    // .then(res => res.json())
    // .then(res=>{
    //     console.log(res);
    // })
    // .catch(err=>alert("失败",err))
    
    // useEffect(()=>{
    //     fetch('http://82.157.94.91:8000/topics/upload',{
    //         method:'post',
    //         body:formdata,
    //         headers:{
    //             'Authorization':'Bearer ' + token,
    //             'Content-type':'multipart/form-data;boundary=01051xcz1000921cksxcs'
    //         }
    //     }).then(res=>{
    //         console.log(res);
    //     }).catch(err=>{
    //         console.log(err)
    //     })
    },[selectImg])
    return (
        <View>
            <Text>hello</Text>
            <Button 
                title='try'
                onPress={pickImage}
            />
        </View>
    )
}

export default TestPage